function euler2 % error using euler method as a function of M when solving % y' = ry(1-y) with y(0) = y0 ' % clear all previous variables and plots clear * clf % parameters for calculation r=10; t0=0; y0=0.01; tmax=1; a0=(1-y0)/y0; % loop used to increase M value for im=1:4 M=10^im; t=linspace(t0,tmax,M+1); h=t(2)-t(1); y=y0; emax=0.0; for i=2:M+1 yy=y+r*h*y*(1-y); ye=1/(1+a0*exp(-r*t(i))); em=abs(yy-ye); if emax < em emax=em; end; y=yy; end; maxerror(im)=emax; pterror(im)=em; points(im)=M; end; % get(gcf) % set(gcf,'position', [1203 757 530 280]); % plot solution loglog(points,pterror,'-rs','markersize',8) hold on loglog(points,maxerror,'-bo','markersize',8) legend(' error at t = 1 ',' Maximum Error ',1); % define axes used in plot xlabel('Number of Time Points','FontSize',14,'FontWeight','bold') ylabel('Error','FontSize',14,'FontWeight','bold') say=['Error Using Euler Method to Solve Logistic Equation']; title(say,'FontSize',14,'FontWeight','bold') % have MATLAB use certain plot options (all are optional) set(gca,'ytick',[1e-6 1e-5 1e-4 1e-3 1e-2 1e-1 1]); grid on set(gca,'MinorGridLineStyle','none') % Set the fontsize to 14 for the plot set(gca,'FontSize',14); % Set legend font to 14/bold set(findobj(gcf,'tag','legend'),'FontSize',14,'FontWeight','bold'); hold off